<template>
  <a-modal title-align="start" :title="props.type == 'goodsVisibility' ? '批量设置可见性' : '批量设置可见范围'" :mask-closable="false" v-model:visible="showvisible" @cancel="showvisible = false" :width="400">
    <a-space>
      <div>{{ props.type == 'goodsVisibility' ? '修改商品可见性' : '修改商品可见范围' }}</div>
      <a-select v-model="code" placeholder="请选择" style="width: 200px">
        <a-option v-if="props.type == 'goodsVisibility'" value="publish">公开</a-option>
        <a-option v-if="props.type == 'goodsVisibility'" value="private">私密</a-option>
        <a-option v-if="props.type != 'goodsVisibility'" value="visible">可见</a-option>
        <a-option v-if="props.type != 'goodsVisibility'" value="search">搜索</a-option>
        <a-option v-if="props.type != 'goodsVisibility'" value="hidden">隐藏</a-option>
      </a-select>
    </a-space>

    <template #footer class="modal-footer">
      <a-button @click="showvisible = false">取消</a-button>
      <a-button type="primary" @click="submit">确定</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
  import { Message } from '@arco-design/web-vue'
  import { onMounted, ref, computed } from 'vue'
  import { batchProduct, versionBatchUpdate } from '@/api/product'
  import _ from 'lodash-es'
  let props = defineProps(['modelValue', 'type', 'ids', 'shopId', 'list'])
  let emits = defineEmits(['save', 'update:modelValue'])
  let showvisible = computed({
    get() {
      return props.modelValue
    },
    set(val) {
      emits('update:modelValue', val)
    },
  })

  let code = ref('')

  const submit = async () => {
    if (!code.value) {
      return Message.error('请选择')
    }
    // let params: any = {
    //   param: {
    //     update: [],
    //   },
    //   shopId: props.shopId,
    // }

    // props.ids.forEach((e: any) => {
    //   let obj: any = {
    //     id: e.id,
    //   }
    //   props.type == 'goodsVisibility' ? (obj.status = code.value) : (obj.catalog_visibility = code.value)
    //   params.param.update.push(obj)
    // })

    // const res: any = await batchProduct(params)

    let arr = _.cloneDeep(props.list)
    arr.forEach((e: any) => {
      props.type == 'goodsVisibility' ? (e.dataFromRestApi.status = code.value) : (e.dataFromRestApi.catalog_visibility = code.value)
    })
    let res: any = null
    res = await versionBatchUpdate({ createOrUpdateDocList: arr })
    if (res) {
      Message.success('修改成功')
      emits('save')
      showvisible.value = false
    }
  }

  onMounted(() => {
    props.type == 'goodsVisibility' ? (code.value = 'publish') : (code.value = 'visible')
  })
</script>

<style lang="less" scoped></style>
